package com.github.llyb120.mugen;

import java.util.Map;

public class PluginLoader extends ClassLoader{

    private PluginOption option = null;
    private Map<String, ByteClass> byteClassMap = null;

    public PluginLoader(ClassLoader parent, PluginOption option, Map<String, ByteClass> byteClassMap) {
        super(parent);
        this.option = option;
        this.byteClassMap = byteClassMap;
    }

    public PluginLoader(PluginOption option, Map<String, ByteClass> byteClassMap) {
        this.option = option;
        this.byteClassMap = byteClassMap;
    }

    @Overrule
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        Class<?> clz = findLoadedClass(name);
        if (clz != null) {
            return clz;
        }
        if (name.startsWith(option.getBasePackage()) && byteClassMap.containsKey(name)) {
            byte[] bs = byteClassMap.get(name).bytes;
            return defineClass(bs, 0, bs.length);
        } else {
            return super.findClass(name);
        }
    }



}
